fix: catch ClosedResourceError when sending error log to disconnected client#2184
Open
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
… client When a client disconnects during request handling, _handle_message's error handler tries to send_log_message() back to the client. Since the write stream is already closed, this raises ClosedResourceError (or BrokenResourceError), which is unhandled and crashes the stateless session with an ExceptionGroup. This is a different code path from what PR modelcontextprotocol#1384 fixed (message router loop). This bug is in the error recovery path: catch exception → try to log it to client → write stream already closed → crash. Added try/except around send_log_message() to catch both ClosedResourceError and BrokenResourceError, logging a debug message instead of crashing. Fixes modelcontextprotocol#2064 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Author
|
Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks! |
Author
|
All CI checks pass. Ready for review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2064
When a client disconnects during request handling in a stateless streamable-HTTP server,
_handle_message's error handler tries to callsend_log_message()back to the client. Since the write stream is already closed, this raisesClosedResourceError, which is unhandled and crashes the stateless session with anExceptionGroup.Root Cause
This is a different code path from what PR #1384 fixed. That PR addressed
ClosedResourceErrorin the message router loop. This bug is in the error recovery path:Fix
Added
try/exceptaroundsend_log_message()in_handle_messageto catch bothClosedResourceErrorandBrokenResourceError, logging a debug message instead of crashing. Failing to notify a disconnected client is expected and harmless.Tests
Added 3 tests to
test_lowlevel_exception_handling.py:test_exception_handling_tolerates_closed_write_stream[closed]— ClosedResourceErrortest_exception_handling_tolerates_closed_write_stream[broken]— BrokenResourceErrortest_exception_handling_reraises_with_closed_stream— original exception still re-raised whenraise_exceptions=TrueAll 9 tests pass.